package attribute;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-10-13
 * Time: 15:00
 */

class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("并重写run方法!");
    }
}

class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("这个Runnable接口");
    }
}

//创建线程的5种方式
public class Demo1 {
    public static void main(String[] args) {

        System.out.println("main线程");

        //1.创建子类继承thread
        Thread t1 = new MyThread();
        t1.start();

        //2.创建子类 实现Runnable接口 在创建MyRunnable实例 传给Thread
        Thread t2 = new Thread(new MyRunnable());
        t2.start();

        //3.是第一，二种方式的变种
        Thread t3 = new Thread(){
            @Override
            public void run() {
                System.out.println("这是第一种创建方式的变种!");
            }
        };
        t3.start();

        //4.
        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Runnable 线程和线程执行的任务更好的解耦 优点是解耦");
            }
        });
        t4.start();

        //5.简便的创建  lambda表达式创建
        Thread t5 = new Thread(() -> {
            System.out.println("hello lambda 不用重写run方法");
        });
        t5.start();
    }
}
